﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TCM.Controllers
{
    public class ExpenseController : Controller
    {
        private TCMContext db = new TCMContext();

        //
        // GET: /Expense/

        public ActionResult Index()
        {
            var Expenses = db.Expenses.Include(e => e.Employee);
            return View(Expenses.ToList());
        }

        //
        // GET: /Expense/Details/5

        public ActionResult Details(Guid? id = null)
        {
            Expense Expense = db.Expenses.Find(id);
            if (Expense == null)
            {
                return HttpNotFound();
            }
            return View(Expense);
        }

        //
        // GET: /Expense/Create

        public ActionResult Create()
        {
            ViewBag.ResponsibleEmployee = new SelectList(db.Employees, "EmployeeID", "NAME");
            ViewBag.StatusID = new SelectList(db.RecordStates
                .ToList().AsEnumerable(), "StatusID", "StatusName");
            ViewBag.ExpenseType = new SelectList(db.ExpenseTypes.ToList().AsEnumerable(), "ExpenseTypeID", "ExpenseTypeName");
            return View();
        }

        //
        // POST: /Expense/Create

        [HttpPost]
        public ActionResult Create(Expense Expense)
        {
            //if (ModelState.IsValid)
            {
                Expense.ExpenseID = Guid.NewGuid();
                db.Expenses.Add(Expense);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ResponsibleEmployee = new SelectList(db.Employees, "EmployeeID", "NAME", Expense.ResponsibleEmployee);
            ViewBag.ExpenseType = new SelectList(db.ExpenseTypes.ToList().AsEnumerable(), "ExpenseTypeID", "ExpenseTypeName");
            ViewBag.StatusID = new SelectList(db.RecordStates
                .ToList().AsEnumerable(), "StatusID", "StatusName");
            return View(Expense);
        }

        //
        // GET: /Expense/Edit/5

        public ActionResult Edit(Guid? id = null)
        {
            Expense Expense = db.Expenses.Find(id);
            if (Expense == null)
            {
                return HttpNotFound();
            }
            ViewBag.ResponsibleEmployee = new SelectList(db.Employees, "EmployeeID", "NAME", Expense.ResponsibleEmployee);
            ViewBag.ExpenseType = new SelectList(db.ExpenseTypes.ToList().AsEnumerable(), "ExpenseTypeID", "ExpenseTypeName");
            ViewBag.StatusID = new SelectList(db.RecordStates
                .ToList().AsEnumerable(), "StatusID", "StatusName", Expense.StatusID);
            return View(Expense);
        }

        //
        // POST: /Expense/Edit/5

        [HttpPost]
        public ActionResult Edit(Expense Expense)
        {
            if (ModelState.IsValid)
            {
                db.Entry(Expense).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ExpenseType = new SelectList(db.ExpenseTypes.ToList().AsEnumerable(), "ExpenseTypeID", "ExpenseTypeName");
            ViewBag.ResponsibleEmployee = new SelectList(db.Employees, "EmployeeID", "NAME", Expense.ResponsibleEmployee);
            ViewBag.StatusID = new SelectList(db.RecordStates
                .ToList().AsEnumerable(), "StatusID", "StatusName", Expense.StatusID);
            return View(Expense);
        }

        //
        // GET: /Expense/Delete/5

        public ActionResult Delete(Guid? id = null)
        {
            Expense Expense = db.Expenses.Find(id);
            if (Expense == null)
            {
                return HttpNotFound();
            }
            return View(Expense);
        }

        //
        // POST: /Expense/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
            Expense Expense = db.Expenses.Find(id);
            db.Expenses.Remove(Expense);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}